package com.yjy.fuyao.stu.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjy.fuyao.stu.entity.Course;
import com.yjy.fuyao.stu.entity.form.CourseInfo;
import com.yjy.fuyao.stu.entity.query.CourseQuery;
import com.yjy.fuyao.stu.entity.vo.CourseWebVo;
import com.yjy.fuyao.stu.entity.vo.PublishCourseVo;
import com.yjy.fuyao.stu.service.ChapterService;
import com.yjy.fuyao.stu.service.CourseService;
import com.yjy.fuyao.utils.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutionException;

@Api(tags="课程管理")
@RestController
@RequestMapping("/admin/stu/course")
public class AdminCourseController {
    @Resource
    private CourseService courseService;

    @Resource
    private ChapterService chapterService;

    @ApiOperation(value = "删除课程")
    @DeleteMapping("/remove")
    public R removeById(
            @ApiParam(name = "id", value = "课程id", required = true)
            @RequestParam("id") String id
    ){
        boolean result = courseService.removeCourseById(id);
        return R.ok();
    }

    @ApiOperation(value = "新增课程")
    @PostMapping("/save/courseInfo")
    public R saveCourseInfo(
            @ApiParam(name = "CourseInfo", value = "课程基本信息", required = true)
            @RequestBody CourseInfo courseInfo){

        String courseId = courseService.saveCourseInfo(courseInfo);
        if(!StringUtils.isEmpty(courseId)){
            return R.ok().data("courseId", courseId);
        }else{
            return R.error().message("保存失败");
        }
    }

    @ApiOperation(value = "获取课程信息")
    @GetMapping("/get/courseInfo/{id}")
    public R getCourseInfo(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable String id
    ){
        System.out.println("获取课程信息的方法");
        CourseInfo courseInfo = courseService.getCourseInfoById(id);
        return R.ok().data("item",courseInfo);
    }


    @ApiOperation(value = "获取待发布课程信息")
    @GetMapping("/get/publishCourseInfo/{id}")
    public R getPublishCourseInfo(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable String id
    ){
        System.out.println("获取待课程信息");
        PublishCourseVo publishCourseVo = null;
        try {
            publishCourseVo = courseService.getPublishCourseInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("获取待课程信息==========="+publishCourseVo);
        return R.ok().data("item", publishCourseVo);
    }

    @ApiOperation(value = "用户发布课程")
    @PutMapping("/publishCourse/{id}")
    public R PublishCourseById(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable String id
    ) throws ExecutionException, InterruptedException {
        boolean result = courseService.publishCourseById(id);
        Course course = courseService.getById(id);
        return R.ok().data("course",course);
    }

    @ApiOperation(value = "管理员发布课程")
    @PutMapping("/adminPublishCourse/{id}")
    public R AdminPublishCourseById(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable String id
    ) throws ExecutionException, InterruptedException {
        courseService.checkCourseById(id,Course.COURSE_PASS);
        return R.ok();
    }

    @ApiOperation(value = "审核课程")
    @PutMapping("/checkCourse/{id}/{checkResult}")
    public R checkCourseById(
            @ApiParam(name = "id", value = "课程id", required = true)
            @PathVariable String id,
            @ApiParam(name = "status", value = "课程状态", required = true)
            @PathVariable String checkResult
    ) throws ExecutionException, InterruptedException {
        boolean result = courseService.checkCourseById(id,checkResult);
        return R.ok();
    }

    @ApiOperation("分页显示课程列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                    CourseQuery courseQuery){
        Page<Course> pageParam = new Page<>(page, limit);
        IPage<Course> pageModel = courseService.listPage(pageParam,courseQuery);
        return R.ok().data("pageModel",pageModel);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "/get/{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId, HttpServletRequest request){
        //课程查询课程基本信息
        CourseWebVo courseInfo = courseService.selectInfoWebById(courseId);
        return R.ok().data("item",courseInfo);
    }

    @GetMapping(value = "/countNewCourse/{day}")
    public R newCourseCount(
            @PathVariable String day){
        Integer count = courseService.countNewCourseByDay(day);
        return R.ok().data("countNewCourse", count);
    }

}
